package cn.ibizlab.odoo.core.odoo_survey.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isurvey_question;
import cn.ibizlab.odoo.core.odoo_survey.domain.Survey_question;
import cn.ibizlab.odoo.core.odoo_survey.filter.Survey_questionSearchContext;
import cn.ibizlab.odoo.core.odoo_survey.service.ISurvey_questionService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[调查问题] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Survey_questionServiceImpl implements ISurvey_questionService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isurvey_questionClientService survey_questionClientService;

    @Override
    public boolean create(Survey_question survey_question) {
        boolean bOk = false ;
        Isurvey_question clientModel = this.convert2Model(survey_question,null);
        survey_questionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,survey_question);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Survey_question survey_question) {
        boolean bOk = false ;
        Isurvey_question clientModel = this.convert2Model(survey_question,null);
        survey_questionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,survey_question) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Survey_question> survey_questions, int batchSize) {

        //survey_questionClientService.updateBatch(survey_questions,batchSize) ;

    }

    @Override
    public void createBatch(List<Survey_question> survey_questions, int batchSize) {

                        //survey_questionClientService.createBatch(survey_questions,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isurvey_question clientModel = survey_questionClientService.createModel();
        clientModel.setId(id);
        survey_questionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Survey_question get(Integer id) {
        Isurvey_question clientModel = survey_questionClientService.createModel();
        clientModel.setId(id);
        survey_questionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Survey_question get(Survey_question survey_question) {
        Survey_question retsurvey_question = this.get(survey_question.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsurvey_question.getClass(), survey_question.getClass(), false);
        copier.copy(retsurvey_question, survey_question, null);
        return survey_question ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //survey_questionClientService.removeBatch(survey_questions) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Survey_question> searchDefault(Survey_questionSearchContext context) {
        List<Survey_question> list = new ArrayList<Survey_question>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_question> clientModelList = survey_questionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_question clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_question>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Survey_question> selectByOdoo_create(Integer id){
        Survey_questionSearchContext context = new Survey_questionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_question> list = new ArrayList<Survey_question>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_question> clientModelList = survey_questionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_question clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_question>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Survey_question> selectByOdoo_write(Integer id){
        Survey_questionSearchContext context = new Survey_questionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_question> list = new ArrayList<Survey_question>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_question> clientModelList = survey_questionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_question clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_question>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据问卷页面查询
     */
    @Override
    public Page<Survey_question> selectByOdoo_page(Integer id){
        Survey_questionSearchContext context = new Survey_questionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Survey_question> list = new ArrayList<Survey_question>();
        Page<cn.ibizlab.odoo.core.client.model.Isurvey_question> clientModelList = survey_questionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isurvey_question clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Survey_question>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isurvey_question convert2Model(Survey_question domain , Isurvey_question model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = survey_questionClientService.createModel() ;
        }

         if(domain.getMatrix_subtypeDirtyFlag())
            model.setMatrix_subtype(domain.getMatrix_subtype());
         if(domain.getValidation_error_msgDirtyFlag())
            model.setValidation_error_msg(domain.getValidation_error_msg());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getComments_allowedDirtyFlag())
            model.setComments_allowed(domain.getComments_allowed());
         if(domain.getValidation_max_dateDirtyFlag())
            model.setValidation_max_date(domain.getValidation_max_date());
         if(domain.getLabels_ids_2DirtyFlag())
            model.setLabels_ids_2(domain.getLabels_ids_2());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getValidation_max_float_valueDirtyFlag())
            model.setValidation_max_float_value(domain.getValidation_max_float_value());
         if(domain.getQuestionDirtyFlag())
            model.setQuestion(domain.getQuestion());
         if(domain.getLabels_idsDirtyFlag())
            model.setLabels_ids(domain.getLabels_ids());
         if(domain.getValidation_min_float_valueDirtyFlag())
            model.setValidation_min_float_value(domain.getValidation_min_float_value());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getConstr_mandatoryDirtyFlag())
            model.setConstr_mandatory(domain.getConstr_mandatory());
         if(domain.getUser_input_line_idsDirtyFlag())
            model.setUser_input_line_ids(domain.getUser_input_line_ids());
         if(domain.getComments_messageDirtyFlag())
            model.setComments_message(domain.getComments_message());
         if(domain.getComment_count_as_answerDirtyFlag())
            model.setComment_count_as_answer(domain.getComment_count_as_answer());
         if(domain.getValidation_requiredDirtyFlag())
            model.setValidation_required(domain.getValidation_required());
         if(domain.getValidation_emailDirtyFlag())
            model.setValidation_email(domain.getValidation_email());
         if(domain.getValidation_min_dateDirtyFlag())
            model.setValidation_min_date(domain.getValidation_min_date());
         if(domain.getConstr_error_msgDirtyFlag())
            model.setConstr_error_msg(domain.getConstr_error_msg());
         if(domain.getValidation_length_maxDirtyFlag())
            model.setValidation_length_max(domain.getValidation_length_max());
         if(domain.getColumn_nbDirtyFlag())
            model.setColumn_nb(domain.getColumn_nb());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getDisplay_modeDirtyFlag())
            model.setDisplay_mode(domain.getDisplay_mode());
         if(domain.getValidation_length_minDirtyFlag())
            model.setValidation_length_min(domain.getValidation_length_min());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getSurvey_idDirtyFlag())
            model.setSurvey_id(domain.getSurvey_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getPage_idDirtyFlag())
            model.setPage_id(domain.getPage_id());
        return model ;
    }

    public Survey_question convert2Domain( Isurvey_question model ,Survey_question domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Survey_question();
        }

        if(model.getMatrix_subtypeDirtyFlag())
            domain.setMatrix_subtype(model.getMatrix_subtype());
        if(model.getValidation_error_msgDirtyFlag())
            domain.setValidation_error_msg(model.getValidation_error_msg());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getComments_allowedDirtyFlag())
            domain.setComments_allowed(model.getComments_allowed());
        if(model.getValidation_max_dateDirtyFlag())
            domain.setValidation_max_date(model.getValidation_max_date());
        if(model.getLabels_ids_2DirtyFlag())
            domain.setLabels_ids_2(model.getLabels_ids_2());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getValidation_max_float_valueDirtyFlag())
            domain.setValidation_max_float_value(model.getValidation_max_float_value());
        if(model.getQuestionDirtyFlag())
            domain.setQuestion(model.getQuestion());
        if(model.getLabels_idsDirtyFlag())
            domain.setLabels_ids(model.getLabels_ids());
        if(model.getValidation_min_float_valueDirtyFlag())
            domain.setValidation_min_float_value(model.getValidation_min_float_value());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getConstr_mandatoryDirtyFlag())
            domain.setConstr_mandatory(model.getConstr_mandatory());
        if(model.getUser_input_line_idsDirtyFlag())
            domain.setUser_input_line_ids(model.getUser_input_line_ids());
        if(model.getComments_messageDirtyFlag())
            domain.setComments_message(model.getComments_message());
        if(model.getComment_count_as_answerDirtyFlag())
            domain.setComment_count_as_answer(model.getComment_count_as_answer());
        if(model.getValidation_requiredDirtyFlag())
            domain.setValidation_required(model.getValidation_required());
        if(model.getValidation_emailDirtyFlag())
            domain.setValidation_email(model.getValidation_email());
        if(model.getValidation_min_dateDirtyFlag())
            domain.setValidation_min_date(model.getValidation_min_date());
        if(model.getConstr_error_msgDirtyFlag())
            domain.setConstr_error_msg(model.getConstr_error_msg());
        if(model.getValidation_length_maxDirtyFlag())
            domain.setValidation_length_max(model.getValidation_length_max());
        if(model.getColumn_nbDirtyFlag())
            domain.setColumn_nb(model.getColumn_nb());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getDisplay_modeDirtyFlag())
            domain.setDisplay_mode(model.getDisplay_mode());
        if(model.getValidation_length_minDirtyFlag())
            domain.setValidation_length_min(model.getValidation_length_min());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getSurvey_idDirtyFlag())
            domain.setSurvey_id(model.getSurvey_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getPage_idDirtyFlag())
            domain.setPage_id(model.getPage_id());
        return domain ;
    }


}
